include(./RV32CrossCompile.cmake)

cmake_minimum_required(VERSION 3.22)
project("rvos" C)
enable_language(ASM)

set(TARGETS "rvos")

set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g2 -ggdb")
set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")

set(CMAKE_VERBOSE_MAKEFILE ON)

##file语法,前一个参数是固定的 后面一个参数自己定义
##添加文件的时候注意 相对路径和绝对路径
file(GLOB_RECURSE C_FILES
    src/*.c
    src/*.h
    src/*.S
)
##比如我使用的rtthread的相关文件不想挤在一起,还可以这样
# file(GLOB_RECURSE KENEL_FILES
#     ${RTTHREAD_SDK}/src/*.c
# )

##cmake语法比较灵活,空格还有换行这些比较随意,相比之下makefile就死板很多,经常出现修改之后makefile文件就报错了
##然后就可以把文件归属为源码
set(SOURCE_FILES  ${C_FILES} ${KENEL_FILES})  ##上面file定义了标识符,有几个写几个就行了,灵活配置
##接下来就是包含头文件路径,比如某些库函数这些,直接设置头文件就行,因为我们一般不会去改他,包含就行
include_directories(

    ${TOOLCHAIN_DIR}/include

    ## rtthread kenel include
    src
)

# set(CMAKE_C_FLAGS "-nostdlib -fno-builtin -march=rv32ima -mabi=ilp32 -g -Wall")
# set(USER_LD_FLAGS "-Ttext=0x80000000") 
# set(ASM_OPTIONS "")
# set(CMAKE_ASM_FLAGS "${CMAKE_C_FLAGS} ${ASM_OPTIONS}")

set(ELF_FILE ${TARGETS}.elf)
set(HEX_FILE ${TARGETS}.hex)
set(BIN_FILE ${TARGETS}.bin)

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Ttext=0x80000000")

# set_target_properties(${ELF_FILE}
#     PROPERTIES
#     LINK_FLAGS /LTCG:incremental
# )

add_executable(${ELF_FILE} ${SOURCE_FILES})

add_custom_command(TARGET ${ELF_FILE} POST_BUILD
    COMMAND ${CMAKE_OBJCOPY} -O binary ${ELF_FILE} ${BIN_FILE}
) 
